/**
 * 
 */
package instant.openvfs.home;

import info.niwota.home.R;
import info.niwota.home.SettingsActivity;
import info.niwota.home.WebBrowser;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;

/**
 * @author qiangli
 * 
 */
public final class AppHelper {

	private AppHelper() {
	}

	public static void startWallpaper(Context ctx) {
		try {
			final Intent i = new Intent(Intent.ACTION_SET_WALLPAPER);
			Intent chooser = Intent.createChooser(i, ctx
					.getString(R.string.menu_wallpaper));
			ctx.startActivity(chooser);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void doSettings(Activity ctx, int rc) {
		try {
			Intent i = new Intent(ctx, SettingsActivity.class);
			ctx.startActivityForResult(i, rc);
		} catch (Exception e) {
			// e.printStackTrace();
		}
	}

	public static void help(Context ctx) {
		try {
			Intent i = new Intent(ctx, WebBrowser.class);
			i.setData(Uri.parse(WebBrowser.HOME_URI));
			ctx.startActivity(i);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void startStartMenuActivity(Context ctx) {
		try {
			Intent i = new Intent();
			i.setAction(Intent.ACTION_MAIN);
			i.setAction(Intent.CATEGORY_LAUNCHER);
			i.setClassName("info.niwota.start",
					"info.niwota.start.StartActivity");
			ctx.startActivity(i);
		} catch (ActivityNotFoundException e) {
			showDownloadStart(ctx);
		} catch (Exception e) {
			Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_SHORT);
		}
	}

	private static void showDownloadStart(final Context ctx) {
		final CharSequence title = "Start by Liqiang";
		final CharSequence msg = "Download Start (free) from Android Market?";
		final CharSequence yes = "Yes";
		final CharSequence no = "No";
		//
		AlertDialog d = new AlertDialog.Builder(ctx).create();
		d.setIcon(android.R.drawable.ic_dialog_info);
		d.setTitle(title);
		d.setMessage(msg);
		d.setButton(yes, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				try {
					Uri uri = Uri
							.parse("market://search?q=pname:info.niwota.start");
					Intent i = new Intent(Intent.ACTION_VIEW, uri);
					ctx.startActivity(i);
				} catch (Exception e) {
					Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_SHORT);
				}
				return;
			}
		});
		d.setButton2(no, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(ctx, "Download cancelled", Toast.LENGTH_SHORT);
				return;
			}
		});
		d.show();
	}

	public static void startViActivity(Context ctx) {
		try {
			Intent i = new Intent();
			i.setAction(Intent.ACTION_MAIN);
			i.setAction(Intent.CATEGORY_LAUNCHER);
			i.setClassName("info.niwota.vi",
					"info.niwota.vi.VoiceInputActivity");
			ctx.startActivity(i);
		} catch (ActivityNotFoundException e) {
			showDownloadVi(ctx);
		} catch (Exception e) {
			Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_SHORT);
		}
	}

	private static void showDownloadVi(final Context ctx) {
		final CharSequence title = "Sphinx VI by Liqiang";
		final CharSequence msg = "Download Sphinx VI (free) from Android Market?";
		final CharSequence yes = "Yes";
		final CharSequence no = "No";
		//
		AlertDialog d = new AlertDialog.Builder(ctx).create();
		d.setIcon(android.R.drawable.ic_dialog_info);
		d.setTitle(title);
		d.setMessage(msg);
		d.setButton(yes, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				try {
					Uri uri = Uri
							.parse("market://search?q=pname:info.niwota.vi");
					Intent i = new Intent(Intent.ACTION_VIEW, uri);
					ctx.startActivity(i);
				} catch (Exception e) {
					Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_SHORT);
				}
				return;
			}
		});
		d.setButton2(no, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(ctx, "Download cancelled", Toast.LENGTH_SHORT);
				return;
			}
		});
		d.show();
	}

	public static Intent createSettingsIntent(Context ctx) {
		// final Intent settings = new
		// Intent(android.provider.Settings.ACTION_SETTINGS);
		final Intent i = new Intent(ctx, SettingsActivity.class);
		i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
				| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
		return i;
	}
}
